<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Goods extends Admin_Controller
{
    private $uid;

    function __construct ()
    {
        parent::__construct();
        $this->load->model('good_m');
        $this->load->model('shop_m');
        $this->load->model('areas_m');
        $this->load->model('goodVouchers_m');
        $this->load->model('category_m');
        $this->load->model('attachs_m');
        $this->load->library('pagination');
        $this->uid = $this->session->userdata('user_id');

    }
    /**
     * 列表
     * [index description]
     * @return [type] [description]
     */
    public function index($page=1)
    {
        $limit = 10;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/goods/index/');
        if($this->auth->is_super_admin()){
            $config['total_rows'] = $this->db->count_all('shop_goods');
        }else{
            $config['total_rows'] = $this->db->where("user_id",$this->uid)->count_all('shop_goods');
        }
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
        if($this->auth->is_super_admin()){
            $goods = $this->good_m->get_all_shop_goods($start,$limit);
        }else{
            $goods = $this->good_m->get_all_shop_goods($start,$limit,$this->uid);
        }
        if(!empty($goods)){
            foreach($goods as $key=>$val){
                $good_category = $this->category_m->get_category_by_id($val['good_category']);
                $goods[$key]['good_category'] = $good_category['category_name'];

                $good_user_info = $this->user_m->get_user_by_user_id($val['user_id']);
                $goods[$key]['user_name'] = $good_user_info['user_name'];
            }
        }
        $data['goods'] = $goods;
        $this->load->view('good/index', $data);

    }
    /**
     * 新增
     * [add description]
     */
    public function add()
    {
        // 获取所有分类
        $category = $this->category_m->get_all_cates();
//        pp($category);exit();
        $data['category2'] = $category;
        $this->load->view('good/form',$data);
    }
    /**
     * 修改产品
     * @param  [type]  $good_id [description]
     * @param  integer $step    [description]
     * @return [type]           [description]
     */
    public function edit($good_id)
    {
        if ($good_id) {
            $good = $this->good_m->get_shop_goods_by_id($good_id);
            // 判断当前产品是是否所属自己
            if(!$this->auth->is_super_admin()){
                if($this->uid != $good['user_id']){
                    $this->logs_m->add_logs($this->uid,"非法修改产品操作");
                    show_message('不允许修改其他人的产品',site_url(X_URL.'good/index'),1);
                    exit();
                }
            }
            $data['good'] = $good;
            $data['good_id'] = $good_id;
            $category = $this->category_m->get_all_cates();
//        pp($category);exit();
            $data['category2'] = $category;
            // 产品图片
            $this->load->view('good/form', $data);
        } else {
            show_message('参数错误',site_url(X_URL.'good/index'),1);
        }

    }
    /**
     * 产品信息
     * [update description]
     * @return [type] [description]
     */
    public function update()
    {
        $post = $this->input->post();
        $goodid = isset($post['good_id']) ? intval($post['good_id']) : false;
        $good_title = isset($post['good_title']) ? htmlspecialchars($post['good_title'], ENT_QUOTES) : false;
        if (!$goodid) {
            // 新增操作，再添加产品信息
            $goods = array(
                'good_title' => $good_title,
                'good_category' => $post['good_category'],
                'good_price'=>$post['good_price'],
                'good_memo'=>$post['good_memo'],
                'good_content'=>$post['good_content'],
                'good_add_time' => time(),
                'good_update_time' => time(),
                'user_id'=>$this->uid
            );
            $goodid = $this->good_m->add_shop_goods($goods);
            if($goodid){
                $this->logs_m->add_logs($this->uid,"新增产品成功");
                show_message('新增产品成功',site_url(X_URL.'goods/index/'),1);
            }else{
                show_message('新增产品失败',site_url(X_URL.'goods/index/'),1);
            }
        } else {
            $info = $this->good_m->get_shop_goods_by_ugid($this->uid,$goodid);
            if(!empty($info)){
                // 修改产品信息
                $goods = array(
                    'good_title' => $good_title,
                    'good_category' => $post['good_category'],
                    'good_price'=>$post['good_price'],
                    'good_memo'=>$post['good_memo'],
                    'good_content'=>$post['good_content'],
                    'good_add_time' => time(),
                    'good_update_time' => time(),
                    'user_id'=>$this->uid,
                );
                $affected_rows = $this->good_m->sava_shop_goods($goodid,$goods);
                if($affected_rows){
                    $this->logs_m->add_logs($this->uid,"修改产品成功");
                    show_message('修改产品成功',site_url(X_URL.'goods/index/'),1);
                }else{
                    show_message('修改产品失败',site_url(X_URL.'goods/index'),1);
                }
            }else{
                show_message('修改产品失败，违规修改，记录一次',site_url(X_URL.'goods/index'),1);
            }
        }
    }

    public function search($page=1){
        $post = $this->input->post();
        $keyword = !empty($post['keyword']) ? $post['keyword'] : "";
        $order = !empty($post['order']) ? $post['order'] : "";
        $limit = 20;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/goods/index/');
        $config['total_rows'] = $this->good_m->get_total_goods_by_keyword($keyword);
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
        
        if(!empty($keyword)){
            $goods = $this->good_m->get_goods_by_keyword($keyword,$order,$start,$limit);
            if(!empty($goods)){
            foreach($goods as $key=>$val){
                $shop = $this->shop_m->get_shop_user_by_id($val['shop_id']);
                $good_category = $this->category_m->get_category_by_id($val['good_category']);
                $good_province =  $this->areas_m->get_area_by_id($val['good_province']);
                $goods[$key]['good_province'] = $good_province['area_name'];
                $good_city =  $this->areas_m->get_area_by_id($val['good_city']);
                $goods[$key]['good_city'] = $good_city['area_name'];
                $good_district =  $this->areas_m->get_area_by_id($val['good_district']);
                $goods[$key]['good_district'] = $good_district['area_name'];
                $goods[$key]['good_category'] = $good_category['category_name'];
                $goods[$key]['shop_title'] = $shop['shop_title'];
            }
        }
         $data['goods'] = $goods;   
        }
        $this->load->view('good/index', $data);
    }
    //删除产品
    public function del($id){
        $goodinfo = $this->good_m->get_shop_goods_by_id($id);
        $good_cover = $goodinfo['good_cover'];
        //删除产品的图片
        $this->attachs_m->delete_attachs_by(['access_key'=>$good_cover]);
        $result = $this->good_m->delete_shop_goods_by_id($id);
        if($result){
            show_message("删除产品成功",site_url(X_URL.'goods/index'),1);
        }
    }

}
